package org.example.iot.aaaaa;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioChannelOption;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.util.Date;
import java.util.Random;
import java.util.concurrent.TimeUnit;

public class Test0x000A111111 {

    //QWGZCJY231218071
    public static void main(String[] args) throws InterruptedException {
        String prefix="batodzx";
        for (int i=1;i<=10;i++){
            NioEventLoopGroup worker = new NioEventLoopGroup();
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.channel(NioSocketChannel.class);
            bootstrap.group(worker);
            bootstrap.option(NioChannelOption.TCP_NODELAY, true);
            int finalI = i;
            bootstrap.handler(new ChannelInitializer<NioSocketChannel>() {
                @Override
                protected void initChannel(NioSocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
                        //和服务器建立连接之后的回调
                        @Override
                        public void channelActive(ChannelHandlerContext ctx) throws Exception {
                            System.out.println("连接建立");
                            //执行登录    02102654660099000180
                            String format = String.format("%09d", finalI);
                            String deviceNo= prefix+format;
                            String deviceNoHex = ByteBufUtil.hexDump(deviceNo.getBytes());
                            byte[] loginBytes = ByteBufUtil.decodeHexDump("02102654660099000180" +
                                    deviceNoHex+
                                    "322e30302e303033310af4205466383635383636303638383135363032343630303837323331303037303038383938363034463231303233443130363730303819000003000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010770d0a");
                            ctx.writeAndFlush(Unpooled.buffer().writeBytes(loginBytes));
                            TimeUnit.SECONDS.sleep(5);


                            ByteBuf byteBuf= ByteBufAllocator.DEFAULT.buffer();
                            //写入尾部
                            byteBuf.writeBytes(ByteBufUtil.decodeHexDump("02ab26546600cb00020a010005004457475a393939323430323231303031020005004457475a393939323430323231303032030005004457475a39393932343032323130303304001e004457475a39393932343032323130303405001e004457475a39393932343032323130303506001e004457475a393939323430323231303036070002044457475a393939323430323231303037080002044457475a393939323430323231303038090002044457475a3939393234303232313030390a0007004457475a393939323430323231303133ca1d0d0a"));
                            ctx.writeAndFlush(byteBuf);

                            /**
                             * 上传数据包
                             */
                            while (true){
                                ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
                                buf.writeBytes(ByteBufUtil.decodeHexDump("02f22454660029000a010a00"));
                                int time = (int) (new Date().getTime() / 1000);
                                buf.writeIntLE(time);
                                Random random = new Random();
                                buf.writeShort(random.nextInt(20));
                                buf.writeShort(random.nextInt(20));

                                buf.writeShort(random.nextInt(20));
                                buf.writeShort(random.nextInt(20));

                                buf.writeShort(random.nextInt(20));
                                buf.writeShort(random.nextInt(20));

                                buf.writeShort(random.nextInt(20));
                                buf.writeShort(random.nextInt(20));

                                buf.writeShort(random.nextInt(20));
                                buf.writeShort(random.nextInt(20));
                                buf.writeBytes(ByteBufUtil.decodeHexDump("61521c0155521c015b551b01b5670d0a"));
                                ctx.writeAndFlush(buf);
                                Thread.sleep(5000);
                            }

                        }
                    });
                }
            });
            ChannelFuture channelFuture = bootstrap.connect("iottcp.caups.cn", 9876).sync();
            Channel channel = channelFuture.channel();
        }

    }

    public static byte[] crc16CCITTFalse(byte[] bytes) {
        int crc = 0xffff; // initial value
        int polynomial = 0x1021; // poly value
        for (int index = 0; index < bytes.length; index++) {
            byte b = bytes[index];
            for (int i = 0; i < 8; i++) {
                boolean bit = ((b >> (7 - i) & 1) == 1);
                boolean c15 = ((crc >> 15 & 1) == 1);
                crc <<= 1;
                if (c15 ^ bit){
                    crc ^= polynomial;
                }
            }
        }
        crc &= 0xffff;
        return new byte[] { (byte) ((0xff00 & crc) >> 8),(byte) (0xff & crc)  };
    }


}
